//
//  PathViewController.swift
//  Animator
//
//  Created by liu guoqiang on 16/9/10.
//  Copyright © 2016年 liu guoqiang. All rights reserved.
//

import UIKit

class PathViewController: UIViewController {
    @IBOutlet weak var ball: UIImageView!
     var flag = 1
    @IBOutlet weak var button: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func click(sender: AnyObject) {
        self.button.alpha = 0.0
        let startPath = CGPathCreateMutable()
        CGPathMoveToPoint(startPath, nil, 160.0, 100.0)
        CGPathAddLineToPoint(startPath, nil, 100.0, 280.0)
        CGPathAddLineToPoint(startPath, nil, 260.0, 170.0)
         CGPathAddLineToPoint(startPath, nil, 60.0, 170.0)
        CGPathAddLineToPoint(startPath, nil, 220.0, 280.0)
        CGPathCloseSubpath(startPath)
        
        let animation = CAKeyframeAnimation(keyPath: "position")
        animation.duration = 5.0
        animation.path = startPath
        animation.delegate = self
        self.ball.layer.addAnimation(animation, forKey: "position")
  
    }
    
    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        UIView.animateWithDuration(1.0) {
            self.button.alpha = 1.0
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
